package cn.edu.scut.sys.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import cn.edu.scut.sys.dao.TrainItemDao;
import cn.edu.scut.sys.service.TrainItemService;
import cn.edu.scut.sys.utils.DataRequest;
import cn.edu.scut.sys.utils.DataResponse;
import cn.edu.scut.sys.vo.TrainItem;

@Component("trainItemService")
public class TrainItemServiceImpl implements TrainItemService{

private TrainItemDao trainItemDao;
	
	@Resource(name="trainItemDao")
	public void setTrainItemDao(TrainItemDao trainItemDao) {
		this.trainItemDao = trainItemDao;
	}

	@Override
	public DataResponse<TrainItem> list(DataRequest request) {
		DataResponse<TrainItem> response = new DataResponse<TrainItem>();  
        int count;//总记录数  
        int limit = request.getRows() <= 0 ? 20 : request.getRows();//每页显示数量  
        int totalPages;//总页数  
        int page = request.getPage() <= 0 ? 1 : request.getPage();//当前显示页码  
        List<TrainItem> list;  
          
        count = trainItemDao.count().intValue();  
        totalPages = count / limit;  
        if (count % limit != 0) {  
            totalPages++;  
        }  
        int currPage = Math.min(totalPages, page);  
          
        int start = currPage * limit - limit;  
        start = start < 0 ? 0 : start;  
          
        list = trainItemDao.list(start, limit, request.getSord(), request.getSidx());  
        response.setRecords(count);  
        response.setTotal(totalPages);  
        response.setPage(currPage);  
        response.setRows(list);  
          
        return response;  
	}

	@Override
	public boolean add(TrainItem trainItem) {
		trainItemDao.insert(trainItem);
		return false;
	}
	
	@Override
	public TrainItem getTrainItem(Long id){
		return trainItemDao.getTrainItem(id);
	}
	
	@Override
	public boolean delTrainItem(int id){
		return trainItemDao.delTrainItem(id);
	}
	
	@Override
	public boolean updateTrainItem(TrainItem trainItem) {
		return trainItemDao.updateTrainItem(trainItem);
	}

	@Override
	public List<TrainItem> getAll() {
		return trainItemDao.getAll();
	}
	
	
}
